const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',//笔记id
    items:'',//笔记详情
    num:'',//爱心个数
    ids:'',
    index:0,
    member_id:null,
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      member_id:wx.$cache.get('member_id'),
      num:wx.$cache.get('userinfo').give_num,
      ids:app.ids
    })
    console.log(app,'App参数');
    let index = this.data.ids.indexOf(Number(options.id))
    console.log(this.data.ids,options.id,index)
    this.setData({
      index
    })
    this.init()
  },

  init(){//笔记详情
    var that = this
    var params = {
      // note_id:that.data.id,
      note_id:that.data.ids[this.data.index],
      member_id:wx.$cache.get('member_id')
    }
    wx.$http.get(wx.$get.note_info,params).then(res=>{
      console.log(res,'笔记详情',this.data.id)
      this.setData({
        items:res
      })
    })
  },


  /*******
   * 下载笔记
   * 
   */
  downloadNote:function(e) {
    
    console.log(e.currentTarget.dataset.id);
    let idid=e.currentTarget.dataset.id;
    let params = {
      id:e.currentTarget.dataset.id
    }
    wx.showLoading({
      title: '下载中',
    })
    
    // 请求下载
    /*
    let that = this;
    const fileExtName = ".pdf";
    const randfile = new Date().getTime() + fileExtName;
    const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;

    wx.$http.get(wx.$get.note_my_list_down,params).then(res=>{
      console.log(res);
      wx.downloadFile({
        url: res,
        filePath: newPath,
        success: function (res) {
        const filePath = res.tempFilePath;
        wx.openDocument({
              filePath: newPath,
              showMenu: true,
              fileType: 'pdf',
              success: function (res) {}
            })
          },
          fail: function (res) {
            wx.hideLoading();
          }
      })
    });
    */
    
   const fileExtName = ".pdf";
   const randfile = new Date().getTime() + fileExtName;
   const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
   
    wx.$http.get(wx.$get.note_my_list_down,params).then(res=>{
      console.log(res);
      wx.downloadFile({
        url: res, //仅为示例，并非真实的资源
        filePath: newPath,
        success: function (res) {
          if (res.statusCode === 200) {
            wx.hideLoading()
            wx.showToast({
              title: '下载成功',
              icon: 'success',
              duration: 2000
            }) 
            const filePath = res.filePath;
            wx.openDocument({
              filePath: filePath,
              showMenu: true,
              fileType: 'pdf',
              success: function (res) {
                console.log('打开文档成功', res)
              }
            })  
          }  
        },
        fail: function (err) {
          console.log(err)
        }  
      })
    })
    
  },




  del(e){//笔记删除
    const {id} = e.currentTarget.dataset
    var that = this
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗',
      success (res) {
        if (res.confirm) {
          var params = {
            note_id:id
          }
          wx.$http.get(wx.$get.note_del,params).then(res=>{
            console.log(res,'删除笔记')
            var pageArr = getCurrentPages();
            var thisPage = pageArr[pageArr.length - 2];
            if(thisPage.route=="pages/myNotes/myNotes"){
              thisPage.mybi()
              thisPage.mybiid()
            }
            wx.navigateBack({
              delta: 1,
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goedit(e){//去修改笔记
    const {id} = e.currentTarget.dataset
    let {type} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/newnote/newnote?bjid='+id+'&type='+type,
    })
  },
  goxiang(){//查看相关题目
    wx.navigateTo({
      url: `/pages/brushTopic/brushTopic?type=4&id=${this.data.items.subject_id}`,
    })
  },
  kanimg(e){//看图片
    let {index} = e.currentTarget.dataset
    wx.previewImage({
      current: this.data.items.imgs[index], // 当前显示图片的http链接
      urls: this.data.items.imgs // 需要预览的图片http链接列表
    })
  },
  shang(){
    console.log(this.data.index,this.data.ids.length)
    if(this.data.index<=0){
      wx.showToast({
        title: '没有上一个',
        icon: 'none',
        duration: 2000
      })
    }else{
      this.setData({
        index:this.data.index-=1
      })
      this.init()
    }
  },
  next(){
    console.log(this.data.index,this.data.ids.length)
    if(this.data.index>=this.data.ids.length-1){
      wx.showToast({
        title: '没有下一个',
        icon: 'none',
        duration: 2000
      })
    }else{
      this.setData({
        index:this.data.index+=1
      })
      this.init()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})